import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getStudents, getClasses } from '@/api/student'

export const useStudentStore = defineStore('student', () => {
  // 状态
  const students = ref([])
  const total = ref(0)
  const classes = ref([])
  const loading = ref(false)

  // 获取学生列表
  async function fetchStudents(params) {
    loading.value = true
    try {
      const res = await getStudents(params)
      students.value = res.data
      total.value = res.pagination.total
      return res
    } finally {
      loading.value = false
    }
  }

  // 获取班级列表
  async function fetchClasses() {
    try {
      const res = await getClasses()
      classes.value = res.data
      return res.data
    } catch (error) {
      console.error('获取班级列表失败:', error)
      return []
    }
  }

  return {
    students,
    total,
    classes,
    loading,
    fetchStudents,
    fetchClasses
  }
})
